package com.example.orderservice_1;

import com.example.orderservice_1.domain.User;
import com.example.orderservice_1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户注册
    @PostMapping("/register")
    public String register(@RequestBody User user) {
        try {
            User registeredUser = userService.register(user);
            return "注册成功，用户ID: " + registeredUser.getId();
        } catch (RuntimeException e) {
            return e.getMessage(); // 返回错误信息
        }
    }

    // 用户登录
    @PostMapping("/login")
    public String login(@RequestBody User user) {
        try {
            User loggedInUser = userService.login(user.getUsername(), user.getPassword());
            return "登录成功，用户ID: " + loggedInUser.getId();
        } catch (RuntimeException e) {
            return e.getMessage(); // 返回错误信息
        }
    }
}
